package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.service.TemplateService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.template.pojo.dto.CompanySaveDTO;
import com.xbongbong.pro.template.pojo.dto.ListTemplateSortDTO;
import com.xbongbong.pro.template.pojo.dto.TemplateChangeDTO;
import com.xbongbong.pro.template.pojo.dto.TemplateDTO;
import com.xbongbong.pro.template.pojo.dto.TemplatePermissionDTO;
import com.xbongbong.pro.template.pojo.dto.TemplatePermissionSetDTO;
import com.xbongbong.pro.template.pojo.vo.CompanyVO;
import com.xbongbong.pro.template.pojo.vo.EnableVO;
import com.xbongbong.pro.template.pojo.vo.TemplateGroupMenuVO;
import com.xbongbong.pro.template.pojo.vo.TemplateListVO;
import com.xbongbong.pro.template.pojo.vo.TemplateMenuVO;
import com.xbongbong.pro.template.pojo.vo.TemplatePermissionVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author bomfish
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/template")
public class TemplateController {
    @Resource
    private TemplateService templateService;

    @RequestMapping(value = "/menu/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid TemplateDTO templateDTO, BindingResult br) throws Exception {
        XbbResponse<TemplateMenuVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TemplateMenuVO templateMenuVO = templateService.getMenuList(templateDTO);
                response = new XbbResponse<>(templateMenuVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取分组的表单
     * @param templateDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "get/menu/group", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getMenuGroup(@RequestBody @Valid TemplateDTO templateDTO, BindingResult br) throws Exception {
        XbbResponse<TemplateGroupMenuVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TemplateGroupMenuVO templateGroupMenuVO = templateService.getMenuGroup(templateDTO);
                response = new XbbResponse<>(templateGroupMenuVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/form/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid TemplateDTO templateDTO, BindingResult br) throws Exception {
        XbbResponse<TemplateListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TemplateListVO templateListVO = templateService.getFormList(templateDTO);
                response = new XbbResponse<>(templateListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取表单权限
     * @param templatePermissionDTO
     * @param br
     * @return
     * @throws Exception
     * 创建时间 2019/6/7 2:37 PM
     * 修改时间 2019/6/7 2:37 PM
     * @author chy
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/form/permission", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String permission(@RequestBody @Valid TemplatePermissionDTO templatePermissionDTO, BindingResult br) throws Exception {
        XbbResponse<TemplatePermissionVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TemplatePermissionVO templatePermissionVO = templateService.getTemplatePermission(templatePermissionDTO);
                response = new XbbResponse<>(templatePermissionVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    @RequestMapping(value = "/form/changeEnable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String changeEnable(@RequestBody @Valid TemplateChangeDTO templateChangeDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = templateService.changeEnable(templateChangeDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/form/getCompanyConfig", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getCompanyConfig(@RequestBody @Valid TemplateDTO templateDTO, BindingResult br) throws Exception {
        XbbResponse<CompanyVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CompanyVO companyVO = templateService.getCompanyConfig(templateDTO);
                response = new XbbResponse<>(companyVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/form/setTemplatePermission", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String setTemplatePermission(@RequestBody @Valid TemplatePermissionSetDTO templatePermissionSetDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = templateService.setTemplatePermission(templatePermissionSetDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/form/setCompanyConfig", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String saveCompanyConfig(@RequestBody @Valid CompanySaveDTO companySaveDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = templateService.setCompanyConfig(companySaveDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/form/enableList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String enableList(@RequestBody @Valid TemplateDTO templateDTO, BindingResult br) throws Exception {
        XbbResponse<EnableVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(templateDTO.getSaasMark(), templateDTO.getBusinessType(), templateDTO.getMenuId(), templateDTO.getDistributorMark());
                EnableVO enableVO = templateService.getEnableList(templateDTO);
                response = new XbbResponse<>(enableVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取所有启用的模板，不过滤使用权限
     * @param templateDTO
     * @param br
     * @return java.lang.String
     * @throws Exception
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/form/all/enableList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String allEnableList(@RequestBody @Valid TemplateDTO templateDTO, BindingResult br) throws Exception {
        XbbResponse<EnableVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(templateDTO.getSaasMark(), templateDTO.getBusinessType(), templateDTO.getMenuId(), templateDTO.getDistributorMark());
                EnableVO enableVO = templateService.getAllEnableList(templateDTO);
                response = new XbbResponse<>(enableVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    //更新表单模排序值
    @RequestMapping(value = "/form/templateSort", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String templateSort(@RequestBody @Valid ListTemplateSortDTO listTemplateSortDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = templateService.updateTemplateSort(listTemplateSortDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/form/templateList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String templateList(@RequestBody @Valid TemplateDTO templateDTO, BindingResult br) throws Exception {
        XbbResponse<EnableVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(templateDTO.getSaasMark(), templateDTO.getBusinessType(), templateDTO.getMenuId(), templateDTO.getDistributorMark());
                EnableVO enableVO = templateService.templateList(templateDTO);
                response = new XbbResponse<>(enableVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    /**
     * 打印查询模板表单
     * @param templateDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/print/templateList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String printTemplateList(@RequestBody @Valid TemplateDTO templateDTO, BindingResult br) throws Exception {
        XbbResponse<EnableVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                EnableVO enableVO = templateService.templateListPrint(templateDTO);
                response = new XbbResponse<>(enableVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    /**
     * 模版数量校验
     * @param templateDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/form/checkNum", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String checkNum(@RequestBody @Valid TemplateDTO templateDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = templateService.checkNum(templateDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
